<template>
    <el-dialog
            :title="!dataForm.leadId ? '新增' : '修改'"
            :close-on-click-modal="false"
            :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
                            <el-form-item label="线索ID，主键" prop="leadId">
                        <el-input v-model.number="dataForm.leadId" placeholder="线索ID，主键"></el-input>
                </el-form-item>
                <el-form-item label="线索来源，如线上广告、线下活动等" prop="leadSource">
                        <el-input v-model="dataForm.leadSource" placeholder="线索来源，如线上广告、线下活动等"></el-input>
                </el-form-item>
                <el-form-item label="线索联系人姓名" prop="leadName">
                        <el-input v-model="dataForm.leadName" placeholder="线索联系人姓名"></el-input>
                </el-form-item>
                <el-form-item label="线索联系人电话" prop="leadPhone">
                        <el-input v-model="dataForm.leadPhone" placeholder="线索联系人电话"></el-input>
                </el-form-item>
                <el-form-item label="线索联系人邮箱" prop="leadEmail">
                        <el-input v-model="dataForm.leadEmail" placeholder="线索联系人邮箱"></el-input>
                </el-form-item>
                <el-form-item label="线索跟进状态" prop="leadStatus">
                        <el-input v-model="dataForm.leadStatus" placeholder="线索跟进状态"></el-input>
                </el-form-item>
                <el-form-item label="分配给的销售人员ID" prop="assignedTo">
                        <el-input v-model.number="dataForm.assignedTo" placeholder="分配给的销售人员ID"></el-input>
                </el-form-item>
                <el-form-item label="线索创建时间" prop="createdAt">
                        <el-date-picker
                                v-model="dataForm.createdAt"
                                type="datetime"
                                placeholder="线索创建时间">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="线索更新时间" prop="updatedAt">
                        <el-date-picker
                                v-model="dataForm.updatedAt"
                                type="datetime"
                                placeholder="线索更新时间">
                        </el-date-picker>
                </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
    export default {
      data () {
        return {
          visible: false,
          dataForm: {
            leadId: null,
            leadSource: null,
            leadName: null,
            leadPhone: null,
            leadEmail: null,
            leadStatus: null,
            assignedTo: null,
            createdAt: null,
            updatedAt: null
          },
          dataRule: {
            leadId: [
                        { required: true, message: '线索ID，主键不能为空', trigger: 'blur' }
            ],
            leadSource: [
                        { required: true, message: '线索来源，如线上广告、线下活动等不能为空', trigger: 'blur' }
            ],
            leadName: [
                        { required: true, message: '线索联系人姓名不能为空', trigger: 'blur' }
            ],
            leadPhone: [
                        { required: true, message: '线索联系人电话不能为空', trigger: 'blur' }
            ],
            leadEmail: [
                        { required: true, message: '线索联系人邮箱不能为空', trigger: 'blur' }
            ],
            leadStatus: [
                        { required: true, message: '线索跟进状态不能为空', trigger: 'blur' }
            ],
            assignedTo: [
                        { required: true, message: '分配给的销售人员ID不能为空', trigger: 'blur' }
            ],
            createdAt: [
                        { required: true, message: '线索创建时间不能为空', trigger: 'blur' }
            ],
            updatedAt: [
                        { required: true, message: '线索更新时间不能为空', trigger: 'blur' }
            ]
          }
        }
      },
      methods: {
        init (id) {
          this.dataForm.leadId = id || 0
          this.visible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
            this.dataForm.leadId = id || 0
            if (this.dataForm.leadId) {
              this.$http({
                url: this.$http.adornUrl(`/app/sales_leads/info/${this.dataForm.leadId}`),
                method: 'get',
                params: this.$http.adornParams()
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.dataForm = data.appsalesleads
                }
              })
            }
          })
        },
            // 表单提交
        dataFormSubmit () {
          this.$refs['dataForm'].validate((valid) => {
            if (valid) {
              this.$http({
                url: this.$http.adornUrl(`/app/sales_leads/${!this.dataForm.leadId ? 'save' : 'update'}`),
                method: 'post',
                data: this.$http.adornData(this.dataForm)
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                      this.visible = false
                      this.$emit('refreshDataList')
                    }
                  })
                } else {
                  this.$message.error(data.msg)
                }
              })
            }
          })
        }
      }
    }
</script>